package com.ucode.springboot.starter.canal.event;

import java.util.List;

import org.springframework.util.CollectionUtils;

import com.alibaba.otter.canal.protocol.CanalEntry.RowChange;
import com.ucode.springboot.starter.canal.option.AbstractDBOption;

public class DealCanalEventListener implements CanalEventListener{

    private AbstractDBOption header;
    
    public DealCanalEventListener(AbstractDBOption... dbOptions) {
        AbstractDBOption tmp = null;
        for (AbstractDBOption dbOption : dbOptions) {
            if (tmp != null) {
                tmp.setNext(dbOption);
            } else {
                this.header = dbOption;
            }
            tmp = dbOption;
        }
    }
    
    public DealCanalEventListener(List<AbstractDBOption> dbOptions) {
        if (CollectionUtils.isEmpty(dbOptions)) {
            return;
        }
        AbstractDBOption tmp = null;
        for (AbstractDBOption dbOption : dbOptions) {
            if (tmp != null) {
                tmp.setNext(dbOption);
            } else {
                this.header = dbOption;
            }
            tmp = dbOption;
        }
    }
    
    @Override
    public void onEvent(String destination, String schemaName, String tableName, RowChange rowChange) {
        this.header.doChain(destination, schemaName, tableName, rowChange);        
    }

}
